package com.ztesoft.sca.dao;

import com.ztesoft.sca.model.AudioSourceDto;
import com.ztesoft.sca.model.AudioSrcQo;
import com.ztesoft.sca.util.variable.NameRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.naming.Name;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author liang.aozhao
 * @created 2018/5/23 10:45
 */
@Repository
public class AudioSrcDaoImpl implements AudioSrcDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public AudioSourceDto getAudioSrcById(Long fileId) throws Exception {
        String sql = "select file_id,set_id,file_name,file_path,server_ip,server_name,server_password,format,sample_rate,descpiption,create_time,request_action,status,call_time,book_time,book_end_time,handup_time,handup_end_time,order_status,order_id from sca_audio_source where file_id = ?";
        AudioSourceDto audioSourceDto = this.jdbcTemplate.queryForObject(sql, new Object[]{fileId}, BeanPropertyRowMapper.newInstance(AudioSourceDto.class));
        return audioSourceDto;
    }

    /**
     *
     * @param audioSrcQo  （当查询条件对象为空时，即是查询所有的）
     * @param startRowNum
     * @param endRowNum
     * @return
     * @throws Exception
     */
    @Override
    public List<AudioSourceDto> listAudioSrc(AudioSrcQo audioSrcQo,int startRowNum,int endRowNum) throws Exception {
        if(audioSrcQo == null) {
            return null;
        }
        StringBuffer sql = new StringBuffer("select file_id,set_id,file_name,file_path,server_ip,server_name,server_password,format,sample_rate,descpiption,create_time,request_action,status,call_time,book_time,book_end_time,handup_time,handup_end_time,order_status,order_id from (select rownum as rowno , t.* from sca_audio_source t where rownum <= ? ");
        List<Object> objectList = new ArrayList<>();
        objectList.add(endRowNum);
        if(audioSrcQo != null ) {
            if(audioSrcQo.getCreateTime() != null ) {
                sql.append(" and create_time < ? ");
                objectList.add(audioSrcQo.getCreateTime());
            }
            if(audioSrcQo.getOrderId() != null){
                sql.append(" and order_id = ? ");
                objectList.add(audioSrcQo.getOrderId());
            }
            if(audioSrcQo.getOrderStatus() != null && !"".equals(audioSrcQo.getOrderStatus())){
                sql.append(" and order_status = ? ");
                objectList.add(audioSrcQo.getOrderStatus());
            }
            if(audioSrcQo.getStatus() != null && !"".equals(audioSrcQo.getStatus())){
                sql.append(" and status = ? ");
                objectList.add(audioSrcQo.getStatus());
            }
            if(audioSrcQo.getFileName() != null && !"".equals(audioSrcQo.getFileName())) {
                sql.append(" and file_name like '%'||?||'%' ");
                objectList.add(audioSrcQo.getFileName());
            }
            if(audioSrcQo.getSetId() != null ){
                sql.append(" and set_id = ? ");
                objectList.add(audioSrcQo.getSetId());
            }
        }
        sql.append(" ) table_alias where table_alias.rowno >= ? ");
        objectList.add(startRowNum);
        Object[] args = objectList.toArray();
        List<AudioSourceDto> audioSourceDtos = this.jdbcTemplate.query(sql.toString(),args,BeanPropertyRowMapper.newInstance(AudioSourceDto.class));
        return audioSourceDtos;
    }

}
